package com.jiahua.user.biz.action.customer;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.struts2.interceptor.SessionAware;

import com.jiahua.user.biz.model.Address;
import com.jiahua.user.biz.model.Customer;
import com.jiahua.user.biz.service.AddressService;
import com.jiahua.user.biz.service.CustomerService;
import com.jiahua.utils.string.StringUtil;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings({ "unused", "rawtypes" })
public class UserRegisterAction extends ActionSupport implements SessionAware {

	private static final long serialVersionUID = -6768835979298798305L;
	Customer customer = new Customer();
	Address address = new Address();

	List<Customer> customerList = new ArrayList<Customer>();

	CustomerService customerService;
	AddressService addressService;

	private Map session = null;
	private String ip;
	private String username;
	private String loginid;
	private String email;
	private String cellphonenumber;
	private String password;
	private String postal;
	private String address1;
	private String address2;
	private String address3;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getLoginid() {
		return loginid;
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public void setLoginid(String loginid) {
		this.loginid = loginid;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getCellphonenumber() {
		return cellphonenumber;
	}

	public void setCellphonenumber(String cellphonenumber) {
		this.cellphonenumber = cellphonenumber;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPostal() {
		return postal;
	}

	public void setPostal(String postal) {
		this.postal = postal;
	}

	public String getAddress1() {
		return address1;
	}

	public void setAddress1(String address1) {
		this.address1 = address1;
	}

	public String getAddress2() {
		return address2;
	}

	public void setAddress2(String address2) {
		this.address2 = address2;
	}

	public String getAddress3() {
		return address3;
	}

	public void setAddress3(String address3) {
		this.address3 = address3;
	}

	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}

	public void setAddressService(AddressService addressService) {
		this.addressService = addressService;
	}

	public List<Customer> getCustomerList() {
		return customerList;
	}

	public void setCustomerList(List<Customer> customerList) {
		this.customerList = customerList;
	}

	@Override
	public String execute() throws Exception {

		// String customerId = UUID.randomUUID().toString();
		// saveCustomer(customerId);

		generatedDumy(3);

		return Action.SUCCESS;
	}

	private boolean saveCustomer(String id) throws Exception {
		customer.setCustomerID(id);
		customer.setZip(Integer.parseInt(postal));
		customer.setPhone(cellphonenumber);
		customer.setLastName(new String(username.getBytes("UTF-8"), "UTF-8"));
		customer.setLoginID(loginid);
		String address = new String((address1 + address2 + address3).getBytes("UTF-8"), "UTF-8");
		customer.setState(address1);
		customer.setCity(address2);
		customer.setInitPassword(password);
		customer.setLoginPassword(password);
		customer.setEmail(email);
		customer.setIp(ip);
		customer.setCountry("日本");
		customer.setVisitNum(1);
		customer.setFlag(1);
		customer.setRegistrationDate(StringUtil.getRegDate());
		customer.setLastLoginDate(StringUtil.getRegDate());
		customerService.addCustomer(customer);

		saveAddress(customer);
		// generatedDumy(50000);
		return true;
	}

	public boolean saveAddress(Customer customer) throws Exception {
		String addressID = UUID.randomUUID().toString();
		address.setAddressID(addressID);
		address.setCustomer(customer);
		address.setAddressNO(1);
		address.setZip(customer.getZip());
		address.setPhone(customer.getPhone());
		address.setState(customer.getState());
		address.setCity(customer.getCity());
		address.setFlag(1);

		addressService.saveAddress(address);
		return true;
	}

	public void generatedDumy(int num) {

		StringUtil util = new StringUtil();
		Customer customer = null;
		for (int i = 0; i < num; i++) {
			customer = new Customer();

			String customerId = UUID.randomUUID().toString();
			String zipCode = StringUtil.genRumdomNumber(7);
			String phoneNum = StringUtil.genPhoneNum();
			String state = util.genKatanaka(12);
			String city = util.genKatanaka(12);
			String addresstemp = util.genKatanaka(12);

			customer.setCustomerID(customerId);
			customer.setZip(Integer.parseInt(zipCode));
			customer.setPhone(StringUtil.genPhoneNum());
			try {
				customer.setLastName(new String(util.genKatanaka(4).getBytes("UTF-8"), "UTF-8"));
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			customer.setLoginID(StringUtil.genRumdomString(6));
			try {
				String address = new String(addresstemp.getBytes("UTF-8"), "UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			customer.setState(state);
			customer.setCity(city);
			customer.setInitPassword(StringUtil.genRumdomString(6));
			customer.setLoginPassword(StringUtil.genRumdomString(6));
			customer.setEmail(StringUtil.genRumdomString(6) + "@gmail.com");
			customer.setIp("127.0.0.1");
			customer.setCountry("日本");
			customer.setVisitNum(1);
			customer.setFlag(Integer.parseInt("1"));
			customer.setRegistrationDate(StringUtil.getRegDate());
			customer.setLastLoginDate(StringUtil.getRegDate());
			customerService.addCustomer(customer);
			
			// save the address
			try {
				saveAddress(customer);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public static void main(String args[]) {

	}

}
